/**
 * 
 */
package com.hcmu.view.MainHome;

import java.util.ArrayList;

import com.telpoo.truyencuoi.R;
import com.hcmu.dto.DatabaseHelper;
import com.hcmu.dto.StoryDTO;
import com.hcmu.menu.MenuActivity;
import com.hcmu.view.custome.StoriesListAdapter;
import com.korovyansk.android.slideout.SlideoutActivity;
import com.markupartist.android.widget.ActionBar;
import com.markupartist.android.widget.ActionBar.AbstractAction;

import android.app.Activity;
import android.app.Dialog;
import android.content.ActivityNotFoundException;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.util.Log;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.ListView;

/**
 * @author SEVEN
 *
 */
public class FavouriteList extends Activity implements OnItemClickListener, OnItemLongClickListener {
	View app;
	ListView listView;
	StoriesListAdapter storiesAdapter;
	ArrayList<StoryDTO> storiesList;
	Dialog optionDialog;
	DatabaseHelper databaseHelper;
	
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		LayoutInflater lf = LayoutInflater.from(this);
		app = lf.inflate(R.layout.layout_favourite_list, null);
		setContentView(app);
		
		ActionBar actionBar = (ActionBar) app.findViewById(R.id.actionbar);
		//actionBar.setHomeAction(new MyIntentAction(this, null, R.drawable.back_icon));
        actionBar.setTitle(getResources().getString(R.string.TITLE_FAVOURITE_STORY_LIST));
        
        databaseHelper = new DatabaseHelper(this);
        
		initListView();
		
		final View[] children = new View[] { app };
	}

	
	private void initListView() {
		listView = (ListView) app.findViewById(R.id.listView);
		//get data
		storiesList = getFavouriteStoryList();
		
		//adapter
		storiesAdapter = new StoriesListAdapter(this, R.id.listView, storiesList);
		listView.setAdapter(storiesAdapter);
		listView.setOnItemClickListener(this);
		listView.setOnItemLongClickListener(this);
		storiesAdapter.notifyDataSetChanged();
	}

	
	@Override
	public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
		int storyid = storiesList.get(position).getId();
		startIntent(storyid, ViewStoryDetailOffline.class);
	}

	@Override
	public boolean onItemLongClick(AdapterView<?> parent, View v, int position,long id) {
		int storyId = storiesList.get(position).getId();
		String storyTitle = storiesList.get(position).getTitle();
		alertOptionListDialog(storyId, storyTitle);
		return false;
	}
	
	
	public ArrayList<StoryDTO> getFavouriteStoryList(){
		ArrayList<StoryDTO> list = new ArrayList<StoryDTO>();
		databaseHelper.openToRead();
		Cursor cursor = databaseHelper.getFavouriteStoryList();
		cursor.moveToFirst();
		for (int i = 0; i < cursor.getCount(); i++) {
			StoryDTO dto = new StoryDTO();
			dto.setIcon(R.drawable.ic_funny);
			dto.setId(Integer.parseInt(cursor.getString(0)));
			dto.setTitle(cursor.getString(1));
			
			list.add(dto);
			cursor.moveToNext();
		}
		databaseHelper.close();
		
		return list;
	}
	
	protected void alertOptionListDialog(final int storyId, String storyTitle) {
		optionDialog = new Dialog(this, 0);
		optionDialog.setTitle(storyTitle);
		optionDialog.setContentView(R.layout.listview);
		optionDialog.show();
		
		final String[] optionlist = getResources().getStringArray(R.array.simple_story_option_list);
		ListView lv = (ListView) optionDialog.findViewById(R.id.listView);
		
		ArrayAdapter<String>  adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, optionlist);
		lv.setAdapter(adapter);
		lv.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View v, int position,
					long id) {
				switch (position) {
					case 0: //view detail
						optionDialog.dismiss();
						startIntent(storyId, ViewStoryDetailOffline.class);
						break;
					case 1: //edit
						optionDialog.dismiss();
						startIntent(storyId, CreateNewStory.class);
						break;
					case 2://delete
						if (deleteStory(storyId)) {
							initListView();
							optionDialog.dismiss();
							Toast.makeText(getApplicationContext(), getResources().getString(R.string.SUCCESSFULLY), Toast.LENGTH_LONG).show();
						}else{
							optionDialog.dismiss();
							Toast.makeText(getApplicationContext(), getResources().getString(R.string.UNSUCCESSFULLY), Toast.LENGTH_LONG).show();
						}
						break;
					case 3://share
						
						break;
					default:
						break;
				}
			}
		});
	}
	
	
	private void startIntent(int id, Class<?> cls){
		Intent it = new Intent(FavouriteList.this, cls);
		it.putExtra("ID", id);
		it.putExtra("isFavourite", true);
		startActivity(it);
	}
	
	public Boolean deleteStory(int sid){
		databaseHelper.openToRead();
		int result = databaseHelper.deleteStory(sid);
		databaseHelper.close();
		return (result==1) ? true : false;
	}

	
	
/*	private  class MyIntentAction extends AbstractAction {
        private Context mContext;
        private Intent mIntent;

        public MyIntentAction(Context context, Intent intent, int drawable) {
            super(drawable);
            mContext = context;
            mIntent = intent;
        }

        @Override
        public void performAction(View view) {
    		startActivity(new Intent(FavouriteList.this, MainHome.class));
        }
    }*/
}
